﻿using System;
using System.Windows.Forms;

using Moe.ECamel.Common.Entities;
using Moe.ECamel.Client.Controllers;
using Moe.ECamel.Client.WinForms.Common;
using Moe.ECamel.Client.WinForms.Forms;

namespace Moe.ECamel.Client.WinForms.Controls
{
	public partial class EmployeeListControl : ECamelUserControl
	{
		private EmployeesController _controller;

		public EmployeeListControl()
		{
			InitializeComponent();
		}

		public void Initialize(EmployeesController controller)
		{
			this._controller = controller;
			this.ListBindingSource.DataSource = _controller.List;
		}
		public void RefreshView()
		{
			// Refresh entity list:
			this._controller.RefreshList();
			// Refresh binding:
			this.ListBindingSource.CurrencyManager.Refresh();
			// Refresh menus:
			RefreshMenus();
			// Refresh manager:
			RefreshManager();
		}
		private void RefreshMenus()
		{
			// Get current entity:
			var entity = (Employee)ListBindingSource.Current;
			// Refresh available controls:
			bool hasValue = (entity != null);
			EntityViewToolStripButton.Enabled = EntityViewToolStripMenuItem.Enabled = hasValue;
			EntityEditToolStripButton.Enabled = EntityEditToolStripMenuItem.Enabled = hasValue;
			UpdateStatusToolStripButton.Enabled = UpdateStatusToolStripMenuItem.Enabled = hasValue;
			bool canSetManager = hasValue && (_controller.Manager == null || _controller.Manager.UserId != entity.UserId);
			SetManagerToolStripButton.Enabled = SetManagerToolStripMenuItem.Enabled = canSetManager;
		}
		private void RefreshManager()
		{
			this.ManagerTextBox.Text = (_controller.Manager == null ? null : _controller.Manager.Username);
		}

		private void AddEntity()
		{
			using (var form = new EmployeeDetailsForm(_controller.NewCreateController()))
			{
				DialogResult result = form.ShowDialog();
				if (result == DialogResult.OK)
					RefreshView();
			}
		}
		private void ViewEntity()
		{
			// Get current entity:
			var entity = (Employee)ListBindingSource.Current;
			if (entity != null)
			{
				using (var form = new EmployeeDetailsForm(_controller.NewViewController(entity)))
				{
					form.ShowDialog();
				}
			}
		}
		private void EditEntity()
		{
			// Get current entity:
			var entity = (Employee)ListBindingSource.Current;
			if (entity != null)
			{
				using (var form = new EmployeeDetailsForm(_controller.NewEditController(entity)))
				{
					DialogResult result = form.ShowDialog();
					if (result == DialogResult.OK)
						RefreshView();
				}
			}
		}
		private void UpdateEmployeeStatus()
		{
			// Get current entity:
			var entity = (Employee)ListBindingSource.Current;
			if (entity == null)
				return;

			// Flip status:
			var error = this._controller.UpdateStatus(entity, !entity.IsActive);
			if (error != null)
				ECamelHelper.ShowMessage(error);

			// Refresh list:
			RefreshView();
		}

		private void SetManager()
		{
			// Get current employee:
			var entity = (Employee)ListBindingSource.Current;
			// Change manager:
			var error = _controller.SetManager(entity);
			if (error != null)
				ECamelHelper.ShowMessage(error);
			else
				RefreshManager();
		}

		private void EntityAddToolStripButton_Click(object sender, EventArgs e)
		{
			AddEntity();
		}
		private void EntityViewToolStripButton_Click(object sender, EventArgs e)
		{
			ViewEntity();
		}
		private void EntityEditToolStripButton_Click(object sender, EventArgs e)
		{
			EditEntity();
		}
		private void UpdateStatusToolStripButton_Click(object sender, EventArgs e)
		{
			UpdateEmployeeStatus();
		}
		private void SetManagerToolStripButton_Click(object sender, EventArgs e)
		{
			SetManager();
		}

		private void EntityViewToolStripMenuItem_Click(object sender, EventArgs e)
		{
			ViewEntity();
		}
		private void EntityAddToolStripMenuItem_Click(object sender, EventArgs e)
		{
			AddEntity();
		}
		private void EntityEditToolStripMenuItem_Click(object sender, EventArgs e)
		{
			EditEntity();
		}
		private void UpdateStatusToolStripMenuItem_Click(object sender, EventArgs e)
		{
			UpdateEmployeeStatus();
		}
		private void SetManagerToolStripMenuItem_Click(object sender, EventArgs e)
		{
			SetManager();
		}

		private void ListDataGridView_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
		{
			ViewEntity();
		}

		private void ListBindingSource_CurrentChanged(object sender, EventArgs e)
		{
			RefreshMenus();
		}
	}
}